/**
 * created by 小卷毛, 2022-02-28
 * Copyright (c) 2022, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.rzico.sbl.R
import com.rzico.sbl.model.FilterData
import com.xinnuo.common.extend.*
import com.xinnuo.common.view.DropPopupWindow
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common.decoration.DividerLRDecoration
import com.xinnuo.slimadapter.SlimAdapter

/**
 * 描 述：报货总经理颜色值
 * 作 者：小卷毛  2022-02-28 09:19
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
open class ColorViewModel : CommonViewModel() {

    private var dropFilter: PopupWindow? = null

    val mTimeList by lazy {
        listOf(
            FilterData("近7天", "1", "近7天"),
            FilterData("本月", "2", "本月"),
            FilterData("自定义月份", "3", "自定义"),
        )
    }

    val mTimeCheckList by lazy {
        listOf(
            FilterData("近7天", "1", "近7天", true),
            FilterData("本月", "2", "本月"),
            FilterData("上月", "3", "上月"),
            FilterData("自定义时间", "4", "自定义"),
        )
    }

    // 首页收入颜色值
    fun incomeColor() = arrayListOf(
        Color.rgb(36, 134, 248),
        Color.rgb(255, 209, 0),
        Color.rgb(37, 214, 255),
        Color.rgb(255, 105, 147)
    )

    // 首页支出颜色值
    fun payoutColor() = arrayListOf(
        Color.rgb(36, 134, 248),
        Color.rgb(37, 214, 255),
        Color.rgb(255, 209, 0),
        Color.rgb(255, 105, 147),
        Color.rgb(167, 95, 234)
    )

    // 首页收入方式颜色值
    fun methodColor() = arrayListOf(
        Color.rgb(13, 208, 105),
        Color.rgb(36, 134, 248),
        Color.rgb(255, 57, 112)
    )

    // 首页订单颜色值
    fun orderColor() = arrayListOf(
        Color.rgb(36, 134, 248),
        Color.rgb(37, 214, 255),
        Color.rgb(255, 209, 0)
    )

    // 商品品牌颜色值
    fun brandColor() = arrayListOf(
        Color.rgb(36, 134, 248),
        Color.rgb(247, 88, 129),
        Color.rgb(37, 214, 255),
        Color.rgb(255, 209, 0),
        Color.rgb(0, 207, 181),
        Color.rgb(167, 95, 234),

        Color.rgb(255, 73, 31),
        Color.rgb(224, 170, 240),
        Color.rgb(144, 227, 169),
        Color.rgb(194, 125, 179),
        Color.rgb(79, 151, 201),
        Color.rgb(19, 102, 120),

        Color.rgb(16, 55, 98),
        Color.rgb(135, 55, 137),
        Color.rgb(237, 53, 149),
        Color.rgb(126, 19, 27),
        Color.rgb(240, 132, 32),
        Color.rgb(183, 213, 59),

        Color.rgb(28, 138, 67),
        Color.rgb(225, 93, 112),
        Color.rgb(207, 30, 98),
        Color.rgb(224, 26, 36),
        Color.rgb(218, 227, 86),
        Color.rgb(139, 93, 60)
    )

    // 空桶总数据颜色值
    fun barrelAllColor() = arrayListOf(
        Color.rgb(36, 134, 248),
        Color.rgb(167, 95, 234)
    )

    // 空桶出库颜色值
    fun barrelOutColor() = arrayListOf(
        Color.rgb(255, 209, 0),
        Color.rgb(0, 207, 181)
    )

    // 空桶入库颜色值
    fun barrelInColor() = arrayListOf(
        Color.rgb(36, 134, 248),
        Color.rgb(37, 214, 255),
        Color.rgb(255, 209, 0),
        Color.rgb(255, 105, 147),
        Color.rgb(167, 95, 234),
        Color.rgb(0, 207, 181),
        Color.rgb(255, 73, 31),
        Color.rgb(224, 170, 240),
        Color.rgb(144, 227, 169)
    )

    // 其他颜色值
    fun color1() = arrayListOf(
        Color.rgb(255, 209, 0),
        Color.rgb(37, 214, 255),
        Color.rgb(255, 105, 147)
    )

    fun color2() = arrayListOf(
        Color.rgb(167, 95, 234),
        Color.rgb(255, 0, 0)
    )

    fun color3() = arrayListOf(
        Color.rgb(255, 209, 0),
        Color.rgb(167, 95, 234),
        Color.rgb(36, 134, 248),
        Color.rgb(247, 88, 129),
        Color.rgb(0, 207, 181),
        Color.rgb(255, 73, 31),
        Color.rgb(37, 214, 255),
        Color.rgb(224, 170, 240),
        Color.rgb(144, 227, 169),
        Color.rgb(19, 102, 120)
    )

    fun color4() = arrayListOf(
        Color.rgb(37, 214, 255),
        Color.rgb(255, 209, 0),
        Color.rgb(255, 105, 147)
    )

    fun color5() = arrayListOf(
        Color.rgb(255, 209, 0),
        Color.rgb(37, 214, 255)
    )

    /**
     * 时间下拉选择框
     */
    fun showDropTimeFilter(
        context: Context,
        hintTV: TextView? = null,
        anchor: ImageView? = null,
        bottom: View,
        list: List<FilterData>,
        listener: (FilterData) -> Unit
    ) {
        dropFilter = object : DropPopupWindow(
            context.inflate(R.layout.pop_layout_filter),
            anchor
        ) {

            override fun afterInitView(view: View) {
                val recyclerView = view.find<RecyclerView>(R.id.pop_container)
                recyclerView.apply {
                    loadLinear()
                    addItemDecoration(DividerLRDecoration(bottom = 0.5f))

                    adapter = SlimAdapter.creator()
                        .register<FilterData>(R.layout.item_pop_list) { jector, bean, _ ->

                            // 解构声明
                            val (title, _, hint, check) = bean

                            jector.apply {
                                text(R.id.item_pop_name, title)
                                textColor(
                                    R.id.item_pop_name,
                                    context.getColorEx(
                                        conditionIf(
                                            check,
                                            R.color.colorAccent,
                                            R.color.black
                                        )
                                    )
                                )
                                visibility(
                                    R.id.item_pop_right,
                                    visibleValue { check }
                                )
                                clicked {
                                    (bean.id != "4")
                                        .condition {
                                            hintTV?.text = hint
                                            list.forEach { it.isChecked = false }
                                            bean.isChecked = true
                                            (adapter as SlimAdapter).notifyDataSetChanged()
                                        }

                                    dropFilter?.dismiss()

                                    delay { listener(bean) }
                                }
                            }
                        }
                        .attachTo(this)

                    (adapter as SlimAdapter).setDataList(list)
                }
            }

        }

        dropFilter?.showAsDropDown(bottom)
    }

    override fun onDestroy(owner: LifecycleOwner) {
        dropFilter = null
    }

}